const Service = require("egg").Service;
const errorLog = require("../utils/errorLog");

class NoticeService extends Service {
  async createNotice(noticeObj) {
    try {
      if (!noticeObj) return false;
      if (!noticeObj.title || !noticeObj.content) return false;
      if (noticeObj.title === "" || noticeObj.content === "") return false;
      if (noticeObj.title.length > 50) return false;

      noticeObj.isShow = 1;
      const result = await this.ctx.model.NoticeModel.create(noticeObj);
      if (result) return result;
      return false;
    } catch (error) {
      errorLog(error, "NoticeService----->createNotice");
      return false;
    }
  }

  async getNoticeList(currentPage, pageSize) {
    try {
      const result = await this.ctx.model.NoticeModel.findAndCountAll({
        where: { isShow: 1 },
        offset: (parseInt(currentPage) - 1) * parseInt(pageSize),
        limit: parseInt(pageSize),
        order: [["id", "DESC"]],
        include: [
          {
            model: this.ctx.model.UsersModel,
            as: "users",
            attributes: ["name"],
          },
        ],
      });
      if (result) {
        // 提取用户名并合并到公告对象中
        result.rows = result.rows.map((notice) => ({
          ...notice.dataValues,
          creator: notice.users ? notice.users.name : "未知用户",
        }));

        // 删除 users 对象
        result.rows = result.rows.map((notice) => {
          delete notice.users;
          return notice;
        });

        return result;
      }

      return false;
    } catch (error) {
      errorLog(error, "NoticeService----->getNoticeList");
      return false;
    }
  }

  async getNoticeListByNoticeTitle(noticeTitle, currentPage, pageSize) {
    try {
      const result = await this.ctx.model.NoticeModel.findAndCountAll({
        where: {
          title: { [this.ctx.app.Sequelize.Op.like]: `%${noticeTitle}%` },
          isShow: 1,
        },
        offset: (parseInt(currentPage) - 1) * parseInt(pageSize),
        limit: parseInt(pageSize),
        order: [["id", "DESC"]],
        include: [
          {
            model: this.ctx.model.UsersModel,
            as: "users",
            attributes: ["name"],
          },
        ],
      });
      if (result) {
        // 提取用户名并合并到公告对象中
        result.rows = result.rows.map((notice) => ({
          ...notice.dataValues,
          creator: notice.users ? notice.users.name : "未知用户",
        }));

        // 删除 users 对象
        result.rows = result.rows.map((notice) => {
          delete notice.users;
          return notice;
        });

        return result;
      }

      return false;
    } catch (error) {
      errorLog(error, "NoticeService----->getNoticeListByNoticeTitle");
      return false;
    }
  }

  async updateNotice(noticeObj) {
    try {
      if (!noticeObj) return false;
      if (!noticeObj.title || !noticeObj.content) return false;
      if (noticeObj.title === "" || noticeObj.content === "") return false;

      const result = await this.ctx.model.NoticeModel.update(
        {
          title: noticeObj.title,
          content: noticeObj.content,
          updatedAt: new Date(),
        },
        {
          where: {
            id: noticeObj.id,
          },
        }
      );
      if (result) return result;
      return false;
    } catch (error) {
      errorLog(error, "NoticeService----->updateNotice");
      return false;
    }
  }

  async deleteNotice(id) {
    try {
      const result = await this.ctx.model.NoticeModel.update(
        {
          isShow: 0,
          updatedAt: new Date(),
        },
        {
          where: {
            id,
          },
        }
      );
      if (result) return result;
      return false;
    } catch (error) {
      errorLog(error, "NoticeService----->deleteNotice");
      return false;
    }
  }
}

module.exports = NoticeService;
